package logger.sd.examples.client;

import org.apache.log4j.Level;
import logger.sd.client.ClientLogger;

public class TokenRingClient {

	// Solicita uma instancia do logger
	public static ClientLogger logger = ClientLogger.getInstance();
	
	private String host;
	private int port;

	private void execute() {
		System.out.println("Iniciando cliente...");
		
		logger.connect(host, port);
		
		logger.setLevel(Level.ALL);

		logger.debug("MSG 001 - inicio");
		logger.error("MSG 002");
		logger.info("MSG 003");
		logger.warn("MSG 004");
		logger.fatal("MSG 005 - final");

		// Finaliza a conexao
		logger.end();

		System.out.println("Fim do teste.");
	}
	
	private void parse(String[] args) {
		boolean noserver = true;
		for(String arg:args) {
			if(arg.startsWith("-server=")) {
				noserver = false;
				String[] nextAddress = arg.substring(8).split(":");
				host = nextAddress[0];
				port = Integer.parseInt(nextAddress[1]);
			}
		}
		if(noserver) {
			host = "localhost";
			port = 6789;
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		TokenRingClient client = new TokenRingClient();	
		client.parse(args);	
		client.execute();
	}

}
